import sys
import time

sys.path.append(r"/data/finance.Source/utils")
sys.path.append(r"/data/finance.Source/Analysis/strategy")
sys.path.append(r"/data/finance.Source/Analysis/strategy/config")

import datetimeutils
import bigbuysell
import selectsql
from Shortemptybase import Bigbs

def createbigsitem(codeitem,sqldunm,dnum):
    lastdate = datetimeutils.getlastday(sqldunm)
    bigbsitem = Bigbs(codeitem,dnum,lastdate)
    # 拿分笔数据
    fbdata = selectsql.selectNum(codeitem,lastdate)
    # 设置大笔买卖对象属性
    bigbsitem.ergodicData(fbdata)
    bigbsitem.summary()

    # 最后一交易日成交量
    kcode = codeitem['xlcode']
    dkdata = selectsql.selectDKline(kcode)
    for r_list in dkdata:
        print(r_list)
        # 设置大笔买卖对象分数
        bigbsitem.setScore(codeitem['zb'],r_list['vol'])

    return bigbsitem

# 创建莫一天的大单对象
def createonddaybigsitem(codeitem,sqldunm):
    lastdate = datetimeutils.getlastday(sqldunm)
    bigbsitem = Bigbs(codeitem,1,lastdate)
    # 拿分笔数据
    fbdata = selectsql.selectoneNum(codeitem,sqldunm)
    # 设置大笔买卖对象属性
    bigbsitem.ergodicData(fbdata)
    bigbsitem.summary()

    # 最后一交易日成交量
    kcode = codeitem['xlcode']
    dkdata = selectsql.selectonedayDKline(kcode,lastdate)
    for r_list in dkdata:
        print(r_list)
        # 设置大笔买卖对象分数
        bigbsitem.setScore(codeitem['zb'],r_list['vol'])

    return bigbsitem


# 取莫一天一个品种总和评分(回测)
def getonedayScore(codeitem,lastdayNum):
    bigbsitem100 = createonddaybigsitem(codeitem["n100"], lastdayNum)
    bigbsitem100.printData()

    bigbsitem300 = createonddaybigsitem(codeitem["n300"], lastdayNum)
    bigbsitem300.printData()

    bigbsitem500 = createonddaybigsitem(codeitem["n500"], lastdayNum)
    bigbsitem500.printData()

    bigbsitem1000 = createonddaybigsitem(codeitem["n1000"], lastdayNum)
    bigbsitem1000.printData()

    onedayScore = bigbsitem100.score*0.35 + bigbsitem300.score*0.25 + bigbsitem500.score*0.30 + bigbsitem1000.score*0.10
    print('今天大单得分: ',onedayScore)
    if onedayScore < -60:
        print(bigbsitem100.cname,'做空')
    elif onedayScore > 60:
        print(bigbsitem100.cname,'做多')
    else:
        print(bigbsitem100.cname, '观望')

    return onedayScore

# 取前几天(1,2,3,5,10)品种总和评分
def getmanydayScore(codeitem,dnum,report_file):
    lastdate = datetimeutils.getlastday(-dnum)
    report_file.write('-------------------'+ str(dnum)+ '--' + str(lastdate) +'-----start-------------\n')
    sqldunm = -dnum
    jydunm = datetimeutils.getjiaoyidnum(dnum)
    bigbsitem100 = createbigsitem(codeitem["n100"], sqldunm,jydunm)
    scorelist100 = bigbsitem100.printData()
    for sl in scorelist100:
        report_file.write(sl + '\n')

    bigbsitem300 = createbigsitem(codeitem["n300"], sqldunm,jydunm)
    scorelist300 = bigbsitem300.printData()
    for sl in scorelist300:
        report_file.write(sl + '\n')

    bigbsitem500 = createbigsitem(codeitem["n500"], sqldunm,jydunm)
    scorelist500 = bigbsitem500.printData()
    for sl in scorelist500:
        report_file.write(sl + '\n')

    bigbsitem1000 = createbigsitem(codeitem["n1000"], sqldunm,jydunm)
    scorelist1000 = bigbsitem1000.printData()
    for sl in scorelist1000:
        report_file.write(sl + '\n')

    onedayScore = bigbsitem100.score*0.35 + bigbsitem300.score*0.25 + bigbsitem500.score*0.30 + bigbsitem1000.score*0.10
    print('今天大单得分: ',onedayScore)
#    print('今天总单数:',bigbsitem100.allNum)
    report_file.write('今天大单得分: ' + str(onedayScore)+ '\n')
    if onedayScore < -60:
        print(bigbsitem100.cname,'做空')
        report_file.write(bigbsitem100.cname + '做空')
    elif onedayScore > 60:
        print(bigbsitem100.cname,'做多')
        report_file.write(bigbsitem100.cname + '做多')
    else:
        print(bigbsitem100.cname, '观望')
        report_file.write(bigbsitem100.cname +  '观望')

    report_file.write('-----------------'+ str(dnum)+ '----' + str(lastdate) +'-----end-------------------\n')
    return onedayScore


def getsumScore(codeitem):
    filedatestr = datetimeutils.gettodayo()
    filename = '../report/' + codeitem["n100"]["code"] + filedatestr + '.txt'
    lastday1 = 1
    lastday2 = 2
    lastday3 = 3
    lastday5 = 5
    lastday10 = 12
    mylocalotime = time.localtime()
    xqj = time.strftime("%w", mylocalotime)
    if xqj == '0' or xqj == '1':
        lastday1 = 3
        lastday2 = 4
        lastday3 = 5
        lastday5 = 7
        lastday10 = 14
    if xqj == '2':
        lastday1 = 1
        lastday2 = 4
        lastday3 = 5
        lastday5 = 7
        lastday10 = 14
    if xqj == '3':
        lastday1 = 1
        lastday2 = 2
        lastday3 = 5
        lastday5 = 7
        lastday10 = 14
    if xqj == '4':
        lastday1 = 2
        lastday2 = 3
        lastday3 = 5
        lastday5 = 7
        lastday10 = 14
    if xqj == '5':
        lastday1 = 1
        lastday2 = 2
        lastday3 = 3
        lastday5 = 7
        lastday10 = 14
    if xqj == '6':
        lastday1 = 2
        lastday2 = 3
        lastday3 = 4
        lastday5 = 8
        lastday10 = 14
    with open(filename, 'w') as report_file:
        print(lastday1,lastday2,lastday3,lastday5,lastday10)
        oneScore = getmanydayScore(codeitem,lastday1,report_file)
        twoScore = getmanydayScore(codeitem,lastday2,report_file)
        freeScore = getmanydayScore(codeitem,lastday3,report_file)
        fiveScore = getmanydayScore(codeitem,lastday5,report_file)
        tenScore = getmanydayScore(codeitem,lastday10,report_file)

        sumScore = oneScore*0.2 +twoScore*0.2 + freeScore*0.2 + fiveScore*0.2 + tenScore*0.2
        print('onedayscore: ',oneScore, 'twoscore: ',twoScore, 'freescore: ', freeScore, 'fivescore: ', fiveScore, 'tenscore: ', tenScore, 'sumscore', sumScore)
        report_file.write('得分汇总: ' + 'onedayscore: ' + str(oneScore) + ' twoscore: ' + str(twoScore)+ ' freescore: ' + str(freeScore) + ' fivescore: ' + str(fiveScore) + ' tenscore: ' + str(tenScore) + ' sumscore '+ str(sumScore)+ '\n')


for codeitem in bigbuysell.codes:
    getsumScore(codeitem)
